# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.test import TestCase

# Create your tests here.

"""
def get_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    ip = x_forwarded_for.split(',')[-1].strip()
    # ip = request.META.get('REMOTE_ADDR', '0.0.0.0')
    return ip


class LoginView(AuthenticationExceptView, WdAPIView):

    def post(self, request, *args, **kwargs):
        data = self.request.data.get('login', None)
        if not data:
            return JsonResponse({"ret": {
                    "c": "7",
                    "d": "parameter missing",
            }})
        manufacturer_id = data.get('id', None)
        authority_code = data.get('ac', None)
        ver = data.get('ver', None)
        timestamps = data.get('t', None)
        if not manufacturer_id or not authority_code or not ver or not timestamps:
            return JsonResponse({"ret": {
                    "c": "7",
                    "d": "parameter missing",
            }})
        if manufacturer_id == MANUFACTURER_DICT['id'] and authority_code == MANUFACTURER_DICT['ac']:
            ip = get_ip(request)
            users = User.objects.filter_active(manufacturer_id=manufacturer_id, authority_code=authority_code,
                                               expire_time__gt=int(time.time())).exclude(ip=ip)
            if users:
                return JsonResponse({"ret": {
                        "c": "7",
                        "d": u"更换ip登陆前请退出之前所有登陆",
                }})
            session = "".join(random.sample(string.uppercase + string.hexdigits, 32))
            expire_time = int(time.time()) + MANUFACTURER_COOKIE_LIVE
            User.objects.create(manufacturer_id=manufacturer_id, authority_code=authority_code, session=session,
                                expire_time=expire_time, ip=ip)
            return JsonResponse({
                "ret": {
                        "c": "0",
                        "d": "",
                        "ver": MANUFACTURER_VER,
                        "ses": session},
                "cmds": [
                    {
                        "cmd": "get_online_status"
                    }
                ]
            })
        else:
            return JsonResponse({"ret": {
                    "c": "7",
                    "d": u"鉴权失败，请重新输入",
            }})


class LogoutView(AuthenticationExceptView, WdAPIView):

    def post(self, request, *args, **kwargs):
        ses = self.request.data.get('ses', None)
        logout = self.request.data.get('logout', None)
        t = self.request.data.get('t', None)
        if not ses or not logout or not t:
            return JsonResponse({"ret": {
                    "c": "11",
                    "d": "parameter missing",
                }
                })
        if logout != "1":
            return JsonResponse({"ret": {
                    "c": "11",
                    "d": "the value of 'logout' was wrong ",
            }
            })
        now = int(time.time())
        ip = get_ip(request)
        # user1 = User.objects.filter_active(session=ses, ip=ip).first()
        user = User.objects.filter_active(session=ses, ip=ip, expire_time__gt=now)
        if not user:
            return JsonResponse({"ret": {
                    "c": "11",
                    "d": "session not exist",
            }
            })
        user.update(expire_time=now)
        return JsonResponse({"ret": {
                "c": "0",
                "d": "",
        }
        })


class BeatView(AuthenticationExceptView, WdAPIView):

    def post(self, request, *args, **kwargs):
        ses = self.request.data.get('ses', None)
        t = self.request.data.get('t', None)
        if not ses or not t:
            return JsonResponse({"ret": {
                    "c": "11",
                    "d": "parameter missing",
                    }
                    })

        now = int(time.time())
        ip = get_ip(request)
        user = User.objects.filter_active(session=ses, ip=ip, expire_time__gt=now).first()
        if not user:
            return JsonResponse({"ret": {
                    "c": "11",
                    "d": "session not exist",
            }
            })
        user.expire_time = now + MANUFACTURER_COOKIE_LIVE
        user.save()
        return JsonResponse({"ret": {
                "c": "0",
                "d": "",
        }
        })


class ElevatorRegistryView(AuthenticationExceptView, WdAPIView):

    def post(self, request, *args, **kwargs):
        ses = self.request.data.get('ses', None)
        er = self.request.data.get('t', None)
        if not ses or not er:
            return JsonResponse({"ret": {
                "er": [
                    {"o": None, "c": None, "d": "parameter missing", "ver": MANUFACTURER_VER, "ses": ses},
                    ]
                }
                })
        now = int(time.time())
        ip = get_ip(request)
        user = User.objects.filter_active(session=ses, ip=ip, expire_time__gt=now).first()
        if not user:
            return JsonResponse({"ret": {
                "er": [
                    {"o": None, "c": None, "d": "session not exist", "ver": MANUFACTURER_VER, "ses": ses},
                ]
            }
            })
        l = []
        for item in er:
            f = item.get('f', None)
            mn = item.get('mn', None)
            o = item.get('o', None)
            e = item.get('e', None)
            t = item.get('t', None)
            s = item.get('s', None)
            tm = item.get('tm', None)
            l.append({'o': o, 'c': 0, 'd': '', 'ver': MANUFACTURER_VER, 'ses': ses})
            # TODO 如何注册 为何注册
        return JsonResponse({"ret": {
            "er": l}
        })


class ElevatorONLIneView(AuthenticationExceptView, WdAPIView):

    def post(self, request, *args, **kwargs):
        ses = self.request.data.get('ses', None)
        eo = self.request.data.get('eo', None)
        if not ses or not eo:
            return JsonResponse({"ret": {
                "eo": [
                    {"f": "", "c": "12", "d": "parameter missing"}
                ]}
            })
        now = int(time.time())
        ip = get_ip(request)
        user = User.objects.filter_active(session=ses, ip=ip, expire_time__gt=now).first()
        if not user:
            return JsonResponse({"ret": {
                "eo": [
                    {"f": "", "c": "12", "d": "session not exist"}
                ]}
            })
        ret = {"ret": {"eo": []}}
        commit = True
        for dic in eo:
            try:
                f = dic.get('f', '')
                tn = dic.get('tn', '0')
                on = dic.get('on', '0')
                t = dic.get('t', '0')
                ElevatorONLIne.objects.update_or_create(defaults={"on": on, "tn": tn, "t": t}, f=f)
            except Exception:
                commit = False
                ret["ret"]["eo"].append({"f": f, "c": "12", "d": "parameter error"})
                continue
        if not commit:
            return JsonResponse(ret)
        else:
            return JsonResponse({"ret": {
                "eo": [
                    {"c": "0", "d": "success"}
                ]}
            })


class ElevatorCollectorView(AuthenticationExceptView, WdAPIView):

    def post(self, request, *args, **kwargs):
        ses = self.request.data.get('ses', None)
        oo = self.request.data.get('oo', None)
        if not ses or not oo:
            return JsonResponse({"ret": {
                "oo": [
                    {"o": "", "c": "12", "d": "parameter missing"}
                    ]}
                    })
        now = int(time.time())
        ip = get_ip(request)
        user = User.objects.filter_active(session=ses, ip=ip, expire_time__gt=now).first()
        if not user:
            return JsonResponse({"ret": {
                "oo": [
                    {"o": "", "c": "12", "d": "session not exist"}
                ]}
            })
        ret = {"ret": {"oo": []}}
        commit = True
        for dic in oo:
            try:
                f = dic.get('f', u'')
                # e = dic.get('e', u'')
                o = dic.get('o', u'')
                t = dic.get('t', u'')
                s = dic.get('s', u'')
                if int(s) == 0:
                    s = False
                elif int(s) == 1:
                    s = True
                else:
                    ret["ret"]["oo"].append({"o": o, "c": "12", "d": "在线状态非法"})
                    commit = False
                    continue
                t1 = dic.get('t1', u'')
                t2 = dic.get('t2', u'')
                ElevatorCollector.objects.update_or_create(defaults={'o': o, 't': t, 's': s, 't1': t1, 't2': t2}, f=f)
            except Exception:
                commit = False
                ret["ret"]["oo"].append({"o": o, "c": "12", "d": "parameter error"})
                continue
        if not commit:
            return JsonResponse(ret)
        else:
            return JsonResponse({"ret": {
                "oo": [
                    {"c": "0", "d": "success"}
                ]}
            })


class ElevatorCollectorOnlineRateView(AuthenticationExceptView, WdAPIView):

    def post(self, request, *args, **kwargs):
        ses = self.request.data.get('ses', None)
        dr = self.request.data.get('dr', None)
        if not ses or not dr:
            return JsonResponse({"ret": {
                "dr": [
                    {"f": "", "c": "12", "d": "parameter missing"},
                        ]}
            })

        now = int(time.time())
        ip = get_ip(request)
        user = User.objects.filter_active(session=ses, ip=ip, expire_time__gt=now).first()
        if not user:
            return JsonResponse({"ret": {
                "dr": [
                    {"f": "", "c": "12", "d": "session not exist"},
                ]}
            })
        ret = {"ret": {"dr": []}}
        commit = True
        for dic in dr:
            try:
                f = dic.get('f', u'')
                t = dic.get('t', u'')
                r = dic.get('r', u'')
                tm = dic.get('tm', u'')
                ElevatorCollectorOnlineRate.objects.update_or_create(defaults={'t': t, 'r': r, 'tm': tm}, f=f)
            except Exception:
                commit = False
                ret["ret"]["dr"].append({"f": f, "c": "12", "d": "parameter error"})
                continue
        if not commit:
            return JsonResponse(ret)
        else:
            return JsonResponse({"ret": {
                "dr": [
                    {"c": "0", "d": "success"}
                ]}
            })


class ElevatorStatusView(AuthenticationExceptView, WdAPIView):

    def post(self, request, *args, **kwargs):
        ses = self.request.data.get('ses', None)
        es = self.request.data.get('es', None)
        if not ses or not es:
            return JsonResponse({"ret": {
                "es": [
                    {"o": "", "c": "12", "d": "parameter missing"}]
                    }
                })
        now = int(time.time())
        ip = get_ip(request)
        user = User.objects.filter_active(session=ses, ip=ip, expire_time__gt=now).first()
        if not user:
            return JsonResponse({"ret": {
                "es": [
                    {"o": "", "c": "12", "d": "session not exist"}]
            }
            })
        ret = {"ret": {"es": []}}
        commit = True
        for dic in es:
            try:
                f = dic.get('f', u'')
                e = dic.get('e', u'')
                o = dic.get('o', u'')
                # u上行信号 只有0 1选择，如果不传就会报错
                u = dic.get('u', 2)
                d = dic.get('d', 2)
                lm = dic.get('lm', 8)
                rs = dic.get('rs', 2)
                od = dic.get('od', 2)
                om = dic.get('om', 2)
                sd = dic.get('sd', 2)
                sm = dic.get('sm', 2)
                ob = dic.get('ob', 2)
                dz = dic.get('dz', 2)
                ol = dic.get('ol', 2)
                fl = dic.get('fl', 2)
                ps = dic.get('ps', 2)
                cp = dic.get('cp', u'')
                rc = dic.get('rc', u'')
                dc = dic.get('dc', u'')
                rt = dic.get('rt', u'')
                t = dic.get('t', u'')
                ElevatorStatus.objects.update_or_create(defaults={'o': o, 'u': u, 'd': d, 'lm': lm, 'rs': rs, 'od': od,
                                                                  'om': om, 'sd': sd, 'sm': sm, 'ob': ob, 'dz': dz,
                                                                  'ol': ol, 'fl': fl, 'ps': ps, 'cp': cp, 'rc': rc,
                                                                  'dc': dc, 'rt': rt, 't': t}, e=e, f=f)
            except Exception:
                commit = False
                ret["ret"]["es"].append({"o": o, "c": "12", "d": "parameter error"})
                continue
            if not commit:
                return JsonResponse(ret)
            else:
                return JsonResponse({"ret": {
                    "es": [
                        {"c": "0", "d": "success"}
                    ]}
                })


# 电梯报警
class ElevatorWarningView(AuthenticationExceptView, WdAPIView):

    def post(self, request, *args, **kwargs):
        ses = self.request.data.get('ses', None)
        ea = self.request.data.get('ea', None)
        if not ses or not ea:
            return JsonResponse({"ret": {
                "ea": [
                    {"o": "", "c": "12", "d": "parameter missing"},
                        ]}
            })

        now = int(time.time())
        ip = get_ip(request)
        user = User.objects.filter_active(session=ses, ip=ip, expire_time__gt=now).first()
        if not user:
            return JsonResponse({"ret": {
                "ea": [
                    {"o": "", "c": "12", "d": "session not exist"},
                ]}
            })
        ret = {"ret": {"ea": []}}
        commit = True
        for dic in ea:
            try:
                f = dic.get('f', u'')
                o = dic.get('o', u'')
                s = dic.get('s', u'')
                a = dic.get('a', u'')
                t = dic.get('t', u'')
                ElevatorWarning.objects.update_or_create(defaults={'t': t, 's': s, 'a': a}, f=f, o=o,)
            except Exception:
                commit = False
                ret["ret"]["ea"].append({"o": o, "c": "12", "d": "parameter error"})
                continue
        if not commit:
            return JsonResponse(ret)
        else:
            return JsonResponse({"ret": {
                "ea": [
                    {"c": "0", "d": "success"}
                ]}
            })


class ElevatorFaultView(AuthenticationExceptView, WdAPIView):

    def post(self, request, *args, **kwargs):
        ses = self.request.data.get('ses', None)
        ef = self.request.data.get('ef', None)
        if not ses or not ef:
            return JsonResponse({"ret": {
                "ef": [
                    {"o": "", "c": "", "d": "parameter missing", "ver": "", "ses": ses}]
                }
            })
        for dic in ef:
            f = dic.get('f', u'')
            e = dic.get('e', u'')
            o = dic.get('o', u'')
            s = dic.get('s', u'')
            a = dic.get('a', u'')
            er = dic.get('er', u'')
            t = dic.get('t', u'')
            ElevatorFault.objects.create(f=f, e=e, o=o, s=s, a=a, er=er, t=t)

        return JsonResponse({"ret": {
            "ef": [
                {"o": "1120", "c": "0", "d": "success", "ver": MANUFACTURER_VER, "ses": ses}]
        }
        })


class ElevatorEventView(AuthenticationExceptView, WdAPIView):

    def post(self, request, *args, **kwargs):
        ses = self.request.data.get('ses', None)
        ee = self.request.data.get('ee', None)
        if not ses or not ee:
            return JsonResponse({"ret": {
                "ee": [
                    {"o": "1120", "c": "12", "d": "parameter missing", "ver": MANUFACTURER_VER, "ses": ses}]
                }
                })
        for dic in ee:
            f = dic.get('f', u'')
            e = dic.get('e', u'')
            o = dic.get('o', u'')
            am = dic.get('am', u'')
            p = dic.get('p', u'')
            ss = dic.get('ss', u'')
            om = dic.get('om', u'')
            rm = dic.get('rm', u'')
            fm = dic.get('fm', u'')
            epm = dic.get('epm', u'')
            em = dic.get('em', u'')
            oc = dic.get('oc', u'')
            t = dic.get('t', u'')
            ElevatorEvent.objects.create(f=f, e=e, o=o, am=am, p=p, ss=ss, om=om, rm=rm, fm=fm, epm=epm, em=em, oc=oc, t=t)
        return JsonResponse({"ret": {
                "ee": [
                    {"o": "1120", "c": "0", "d": "success", "ver": MANUFACTURER_VER, "ses": ses}]
                }
                })


class ElevatorStatisticsView(AuthenticationExceptView, WdAPIView):

    def post(self, request, *args, **kwargs):
        ses = self.request.data.get('ses', None)
        est = self.request.data.get('est', None)
        if not ses or not est:
            return JsonResponse({"ret": {
                "est": [
                    {"e": "", "c": "12", "d": "parameter missing", "ver": MANUFACTURER_VER, "ses": ses}]
                    }
                })
        l = []
        for dic in est:
            f = dic.get('f', u'')
            e = dic.get('e', u'')
            bc = dic.get('bc', u'')
            rc = dic.get('rc', u'')
            dc = dic.get('dc', u'')
            rt = dic.get('rt', u'')
            t = dic.get('t', u'')
            ElevatorStatistics.objects.update_or_create(defaults={'bc': bc, 'rc': rc, 'dc': dc, 'rt': rt, 't': t},
                                                        f=f, e=e)
            l.append({'e': e, 'c': '0', 'd': 'success', 'ver': MANUFACTURER_VER, 'ses': ses})
        return JsonResponse({"ret": {
            "est": l
        }
        })
        

转发脚本
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from flask import Flask, request
import redis
import requests
import json


app = Flask(__name__)
base_url = "http://www.wxhcxx.com:8080/"

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>', methods=['POST'])
def test(path):
    url = base_url + path
    data = request.get_json()
    r = requests.post(url, json=data)
    return  json.dumps(r.json())


if __name__ == '__main__':
    app.run(host="0.0.0.0", port="9999")


"""


